//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "VCSizeChosedViewController.h"
@interface VCSizeChosedViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@property(nonatomic, strong) NSArray                     *modelArray;/**< 数据源 */
@property(nonatomic, strong) NSMutableArray                     *itemArray;/**< 按钮数组 */
@property(nonatomic, strong) NSMutableArray                     *titleArray;/**< title数组 */
@property(nonatomic, strong) UIScrollView                     *scrollView;/**< <##> */
@property(nonatomic, strong) UISlider                     *proportionSlider;/**< 间距 slider */
@property(nonatomic, strong) UISlider                     *sizeSlider;/**< size slider */
@property(nonatomic, strong) UISlider                     *roundedSlider;/**< 圆角 slider */
@end
static NSString * buttonModelKey = @"buttonModelKey";

@implementation VCSizeChosedViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth, 300)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    self.customView.centerY = UIScreenHeight / 2;
    [self.view addSubview:self.customView];
    

    STButton * closeButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"down1"]];
    closeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    closeButton.right = self.customView.width;
    [closeButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.customView addSubview:closeButton];
    
    NSArray * array = [VCItemTools allSizeArray];
    self.modelArray = array;
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, closeButton.bottom, UIScreenWidth, 100)];
    scrollView.backgroundColor = UIColor.whiteColor;
    scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView = scrollView;
    [self.customView addSubview:scrollView];
    self.itemArray = NSMutableArray.new;
    self.titleArray = NSMutableArray.new;
    CGFloat left = 20;
    for (NSInteger i = 0 ; i < array.count; i ++) {
        VCItemSizeModel * model  = array[i];
        model.insetValue = self.currentModel.insetValue;
        STButton * sizeButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)
                                                         title:nil
                                                    titleColor:nil
                                                     titleFont:0
                                                  cornerRadius:4
                                               backgroundColor:[UIColor colorWithRed:195/255.0 green:200/255.0 blue:216/255.0 alpha:1]
                                               backgroundImage:nil
                                                         image:nil];
        [sizeButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelctedSizeButton:sender];
        }];
        sizeButton.left = left;
        sizeButton.size = model.size;
        sizeButton.tag = 10000 + i;
        sizeButton.centerY = (scrollView.height - 20)/2 ;
        objc_setAssociatedObject(sizeButton, &buttonModelKey, model, OBJC_ASSOCIATION_RETAIN);
        [scrollView addSubview:sizeButton];
        [self.itemArray addObject:sizeButton];
        left = sizeButton.right + 20;
        
        
        
        STButton * titleButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, sizeButton.width, 15)
                                                         title:model.bili
                                                    titleColor:ThirdTextColor
                                                     titleFont:12
                                                  cornerRadius:0
                                               backgroundColor:nil
                                               backgroundImage:nil
                                                         image:nil];
        [titleButton setTitleColor:TM_redColor forState:UIControlStateSelected];
        titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [scrollView addSubview:titleButton];
         [self.titleArray addObject:titleButton];
        titleButton.centerX = sizeButton.centerX;
        titleButton.bottom = scrollView.height;
        scrollView.contentSize = CGSizeMake(sizeButton.right + 20, 0);
        
        if (i == self.currentModel.index) {
            titleButton.selected = YES;
            sizeButton.selected = YES;
             [sizeButton st_setBorderWith:1 borderColor:TM_redColor cornerRadius:4];
        }
    }
    
    STLabel * proLable = [self deflutLableWithTitle:@"Proportion"];
    proLable.top = scrollView.bottom + 20;
    [self.customView addSubview:proLable];
    
    self.proportionSlider = [self defultSlider];
    self.proportionSlider.minimumValue = 0;
    self.proportionSlider.maximumValue = 20;
    self.proportionSlider.value = self.currentModel.insetValue;
    self.proportionSlider.centerY = proLable.centerY;
    [self.customView addSubview:self.proportionSlider];
    
    
    STLabel * sizeLable = [self deflutLableWithTitle:@"Size"];
    sizeLable.top = proLable.bottom;
    [self.customView addSubview:sizeLable];
    self.sizeSlider = [self defultSlider];
    self.sizeSlider.minimumValue = 0;
    self.sizeSlider.maximumValue = self.modelArray.count - 1;
    self.sizeSlider.value = self.currentModel.index;
    self.sizeSlider.centerY = sizeLable.centerY;
    [self.customView addSubview:self.sizeSlider];
    
    
    STLabel * roundLable = [self deflutLableWithTitle:@"Rounded"];
    roundLable.top = sizeLable.bottom;
    [self.customView addSubview:roundLable];
    
    self.roundedSlider = [self defultSlider];
    self.roundedSlider.minimumValue = 0;
    self.roundedSlider.maximumValue = 0.8 * UIScreenWidth / 2;
    self.roundedSlider.value = self.currentModel.cornerRadius;
    self.roundedSlider.centerY = roundLable.centerY;
    [self.customView addSubview:self.roundedSlider];
    
    
    [self updateSlectedModel];
    
    self.customView.height = roundLable.bottom + 20;
    self.customView.top = UIScreenHeight ;
    
    
}
- (STLabel*)deflutLableWithTitle:(NSString*)title{
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, 100, 40)
                                                     text:title
                                                textColor:SecendTextColor
                                                     font:16
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    return titleLable;
}
- (UISlider*)defultSlider{
    UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(120, 0, UIScreenWidth - 120 - 10, 20)];
    slider.backgroundColor = UIColor.whiteColor;
    slider.thumbTintColor = TM_redColor;
    slider.minimumTrackTintColor = TM_redColor;
    slider.maximumTrackTintColor = FlatGray;
    [slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    return slider;
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.bottom = UIScreenHeight;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method

- (void)onSelectedCancleButton{
    
    [self dismiss];
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}
//更新选中的按钮 并且滑动到 这个地方
- (void)updateSlectedModel{

    for (NSInteger i = 0 ;i < self.itemArray.count;i++ ) {
        UIButton * button = self.itemArray[i];
        UIButton * titlebutton = self.titleArray[i];
        button.selected = NO;
        if (self.currentModel.index == i) {
            button.selected = YES;
            [self.scrollView setContentOffset:CGPointMake(button.left - 20, 0) animated:YES];
        }
        titlebutton.selected = button.selected;
        if (button.selected) {
            [button st_setBorderWith:1 borderColor:TM_redColor cornerRadius:4];
        }else{
            [button st_setBorderWith:1 borderColor:UIColor.clearColor cornerRadius:4];
        }
        
    }
}
#pragma mark --Action Method
- (void)onSelctedSizeButton:(UIButton*)sender{
   VCItemSizeModel * model =  objc_getAssociatedObject(sender, &buttonModelKey);
    model.cornerRadius = self.currentModel.cornerRadius;
    model.insetValue = self.currentModel.insetValue;
    self.currentModel = model;
    self.currentModel.isSizeChanged = YES;
    if (self.didSelctedSizeHandle) {
        self.didSelctedSizeHandle(model);
    }
    for (NSInteger i = 0 ;i < self.itemArray.count;i++ ) {
        UIButton * button = self.itemArray[i];
        UIButton * titlebutton = self.titleArray[i];
        button.selected = NO;
        sender.selected = YES;
        titlebutton.selected = button.selected;
        if (button.selected) {
            [self.sizeSlider setValue:i animated:NO];
            [button st_setBorderWith:1 borderColor:TM_redColor cornerRadius:4];
        }else{
             [button st_setBorderWith:1 borderColor:UIColor.clearColor cornerRadius:4];
        }
        
    }
}
- (void)sliderValueChanged:(UISlider*)slider{
    if (slider == self.proportionSlider) {
        self.currentModel.insetValue = slider.value;
        self.currentModel.isSizeChanged = NO;
    }
    if (slider == self.sizeSlider) {
        NSInteger index = @(slider.value).integerValue;
        self.currentModel = self.modelArray[index];
        self.currentModel.insetValue = self.proportionSlider.value;
        self.currentModel.cornerRadius = self.roundedSlider.value;
        self.currentModel.isSizeChanged = YES;
        [self updateSlectedModel];
    }
    if (slider == self.roundedSlider) {
         self.currentModel.cornerRadius = slider.value;
        self.currentModel.isSizeChanged = NO;
    }
    if (self.didSelctedSizeHandle) {
        self.didSelctedSizeHandle(self.currentModel);
    }
}
@end







